package com.thssh.ui.lsn04.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.thssh.ui.R;

/**
 * 放大镜效果
 *
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/13
 * email: zhangyugehu@163.com
 */

public class ZoomImageView extends View {

    private int mWidth;
    private int mHeight;

    private Paint mSrcPaint;
    private Paint mZoomPaint;

    private Bitmap mSrcBitmap;
    private Bitmap mZoomBitmap;

    private int factor = 3;
    private int radius = 200;
    private Matrix matrix;
    private BitmapShader bitmapShader;

    private int mX;
    private int mY;

    private boolean showZoom;

    public ZoomImageView(Context context) {
        this(context, null);
    }

    public ZoomImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mSrcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mZoomPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSrcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_9);
        mZoomBitmap = mSrcBitmap;
        mZoomBitmap = Bitmap.createScaledBitmap(mZoomBitmap, mZoomBitmap.getWidth() * factor, mZoomBitmap.getHeight() * factor, true);

        matrix = new Matrix();
        bitmapShader = new BitmapShader(mZoomBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mZoomPaint.setShader(bitmapShader);

        Log.d("ZoomImageView", "mSrcBitmap: " + mSrcBitmap.getWidth() + "x" + mSrcBitmap.getHeight());
        Log.d("ZoomImageView", "mZoomBitmap: " + mZoomBitmap.getWidth() + "x" + mZoomBitmap.getHeight());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSrcBitmap(canvas);
        if (showZoom){
            drawZoomBitmap(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mX = (int) event.getX();
                mY = (int) event.getY();
                showZoom = true;
                break;
            case MotionEvent.ACTION_MOVE:
                mX = (int) event.getX();
                mY = (int) event.getY();
                break;
            case MotionEvent.ACTION_UP:
                showZoom = false;
                break;
            default:
                break;
        }
        invalidate();
        return true;
    }

    private void drawZoomBitmap(Canvas canvas) {

        Log.d("ZoomImageView", mX + "x" + mY);
        Log.d("ZoomImageView", matrix.toString());
        RectF rectF = new RectF();

        int left = mX - radius;
        int top = mY - 2*radius;
        if(top < 0){
            top = mY;
        }
        int right = left + 2*radius;
        int bottom = top + 2 * radius;
        rectF.set(left, top, right, bottom );
        int transX = mX;
        int transY = mY;
        matrix.setTranslate((1 - factor) * transX,  (1 - factor) * transY);
        bitmapShader.setLocalMatrix(matrix);
        canvas.drawOval(rectF, mZoomPaint);
    }

    private void drawSrcBitmap(Canvas canvas) {
        canvas.drawBitmap(mSrcBitmap, 0, 0, mSrcPaint);
    }
}
